#include "ResponseAttributeEditor.h"
#include "../core/src/container/ResponseAttribute.h"

namespace Xps
{
	ResponseAttributeEditor::ResponseAttributeEditor(QWidget *parent) : Editor(parent)
	{
		setObjectName("ResponseAttributeEditor");
		QGridLayout* content_layout = new QGridLayout();
		
		
		slider = new QSlider(Qt::Horizontal);
		text = new QLineEdit();
				
		slider->setRange(-100,100);
		content_layout->addWidget(slider,0, 0);
		content_layout->addWidget(text, 0, 1);
		//content_layout->setMargin(0);
		text->setText(QString("%1").arg(0));
		text->setFixedWidth(40);
		text->setValidator(new QIntValidator(-200,200,text));

		connect(slider, SIGNAL(valueChanged(int)), this, SLOT(valueChanged(int)));
		connect(text, SIGNAL(textChanged(QString)), this, SLOT(textChanged(QString)));
		
		setLayout(content_layout);
		
	}

	void ResponseAttributeEditor::read(XpsAssets::AssetData* ad)
	{
		XpsAssets::ResponseAttribute* ra = (XpsAssets::ResponseAttribute*) ad;

		int val = ra->value();
		text->setText(QString("%1").arg(val));
	}

	void ResponseAttributeEditor::write(XpsAssets::AssetData* ad)
	{
		XpsAssets::ResponseAttribute* ra = (XpsAssets::ResponseAttribute*) ad;

		ra->setValue(text->text().toInt());
	}

	void ResponseAttributeEditor::valueChanged(int value)
	{
		text->setText(QString("%1").arg(value));
	}

	void ResponseAttributeEditor::textChanged(QString text)
	{
		slider->setValue(text.toInt());
	}
}